package com.hxzy.filter;

import javax.servlet.*;
import java.io.IOException;

/**
 * 设置请求和响应编码方式的过滤器
 * @author nick
 */
public class EncodingFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        //ServletRequest是HttpServletRequest的父级接口
        //ServletResponse是HttpServletResponse的父级接口
        //第一步：设置request和response的请求编码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        System.out.println("=================进入encoding filter过滤器===============");

        //过滤器有两种选择：1.放行  2.不放行
        //放行只需要调用chain.doFilter()方法就可以了
        chain.doFilter(request,response);
        //如果不调用chain.doFilter(),需要在此处通过response对象响应用户相关信息

        System.out.println("==============encoding filter过滤器响应客户端=============");
    }

    @Override
    public void destroy() {

    }
}
